<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/2/2 0002
 * Time: 11:22
 */

namespace app\index\controller;


use app\index\model\AuthGroup;
use app\index\model\AuthGroupAccess;
use app\index\model\AuthRule;

class Group extends Base
{
    private $model;
    public function initialize()
    {
        parent::initialize();
        $this->model = new AuthGroup();
    }

    public function index()
    {
        return $this->fetch();
    }

    public function lst()
    {
        $start = $this->request->get('start', 0);
        $length  = $this->request->get('length', 15);
        $search  = $this->request->get('keyword');
        $where = [];
        if ($search) {
            $where[] = ['title', 'like', "%{$search}%"];
        }
        $data = $this->model->getList($start, $length, $where);
        $total = $this->model->getListCount($where);
        $result = [
            'draw' => $this->request->get('draw'),
            'recordsTotal' => $total,
            'recordsFiltered' => $total,
            'data' => $data
        ];
        return json($result);
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post('data/a', []);
            $result = $this->validate($data, 'app\index\validate\Group');
            if ($result !== true) {
                $this->error($result);
            }
            $res = $this->model->add($data);
            if ($res !== false) {
                $this->success('添加成功');
            }
            $this->error('添加失败');
        }
        $authRule = new AuthRule();
        $rules = $authRule->getList(0, 1);
        foreach ($rules as $k => $v) {
            $rules[$k]['name'] = $v['title'];
            $rules[$k]['title'] = $v['name'];
        }
        $rules = getTree1($rules);
        foreach ($rules as $k => $v) {
            $rules[$k]['cascade'] = true;
        }
        $this->assign('rules', json_encode($rules));
        return $this->fetch();
    }

    public function edit()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post('data/a', []);
            $id = $this->request->post('id', 0);
            $result = $this->validate($data, 'app\index\validate\Group');
            if ($result !== true) {
                $this->error($result);
            }
            $res = $this->model->edit($id, $data);
            if ($res !== false) {
                $this->success('编辑成功');
            }
            $this->error('编辑失败');
        }
        $id = $this->request->get('id');
        $group = $this->model->getOneById($id);

        $authRule = new AuthRule();
        $rules = $authRule->getList(0, 1);
        foreach ($rules as $k => $v) {
            $rules[$k]['name'] = $v['title'];
            $rules[$k]['title'] = $v['name'];
        }
        $rules = getTree1($rules);
        foreach ($rules as $k => $v) {
            $rules[$k]['cascade'] = true;
        }
        $this->assign('rules', json_encode($rules));
        $this->assign('group', $group);
        return $this->fetch();
    }

    public function del()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->get('id');
            $auth_group_access = new AuthGroupAccess();
            if ($auth_group_access->getCount($id)) {
                $this->error('分组下有管理员不能删除');
            }
            $res = $this->model->del($id);
            if ($res !== false) {
                $this->success('删除成功');
            }
            $this->error('删除失败');
        }
    }
}